import { loadModules } from "esri-loader";

import TDT from "./tdt"; //引入天地图底图模块

//esri-loader一些基本配置
const options = {
  version: "3.34",
  css: true,
  url: "https://js.arcgis.com/3.34/init.js",
}; 

//自定义的参数
let local = {
  map: null,
  mapDivId: "viewDiv",
};

//创建地图
function createMap() {
  //使用Promise对象构建（也可以不用）
  return new Promise((resolve) => {
    loadModules(["esri/map","esri/SpatialReference","esri/layers/ArcGISDynamicMapServiceLayer"], options).then(([Map,SpatialReference,ArcGISDynamicMapServiceLayer]) => {
        //创建地图容器
      local.map = new Map(local.mapDivId, {
        //basemap: "streets", //默认的ESRI streets底图
        zoom: 4,
        center: [116.085777, 39.489784],
        spatialReference: new SpatialReference({ wkid: 4326 }),
        //layers: [myTiledMapServiceLayer], //低图的图层，我这里加入的是上一步定义的图层
      });

      //添加天地图底图
      TDT().then((TDTLayer) => {

        //自定义的地图服务 吐哈油田
        const dyServer = "https://www.igiseis.com:6443/arcgis/rest/services/TuhaService/GISMap_Obstacle/MapServer";
        //加载地图服务
        // 不存在缓存 ， 则用ArcGISDynamicMapServiceLayer
        var myTiledMapServiceLayer = new ArcGISDynamicMapServiceLayer(
            dyServer
        );

        let vec = new TDTLayer({ type: "vec" });
        let cva = new TDTLayer({ type: "cva" });         

        //local.map.addLayer(myTiledMapServiceLayer);
        local.map.addLayer(vec);
        local.map.addLayer(cva);

        //添加影像底图
        // let img= new TDTLayer({ type: "img" });
        // local.map.addLayer(img);
      });

      //监听鼠标点击地图事件
      local.map.on("click", (evt) => {
        let point = evt.mapPoint;
        alert(`x=${point.x}\ny=${point.y}`);
      });

      resolve(true);
    });
  });
}

//对外提供访问
export default {
  createMap,
};
